%%-------------------------------------------------
%% @Module	:	test
%% @Description	:	测试数据库模块
%% 数据库配置请参考emysql.config, 当修改了连接池ID后应在db模块同步修改
%%	CREATE TABLE `user`(
%%		`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增长ID',
%%		`account` char(32) NOT NULL default '[]' COMMENT '用户名',
%%		`passwd` char(32) NOT NULL default '[]' COMMENT '密码',
%%		PRIMARY KEY(`id`),
%%		index account_passwd(`account`, `passwd`)
%%	) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';
%%-------------------------------------------------
-module(test).
-include("log.hrl").
-compile(export_all).
-export([
	test_get_row/0,
	test_get_all/0,
	test_execute/0,
	test_transaction_ok/0,
	test_transaction_fail/0
]).

test_get_row() ->
	Data = db:get_row(<<"SELECT * FROM user">>),
	?ERROR_MSG("test error ~p", [Data]),
	Data.

test_get_all() ->
	db:get_all(<<"SELECT * FROM user">>).

test_execute() ->
	db:execute(<<"INSERT INTO user(account, passwd) VALUES('test', '123456')">>).

test_transaction_ok() ->
	F1 = fun() ->
			db:execute(<<"INSERT INTO user(account, passwd) VALUES('test11', '123456')">>),
			db:execute(<<"INSERT INTO user(account, passwd) VALUES('test12', '123456')">>)
	end,
	db:transaction(F1).

test_transaction_fail() ->
	F2 = fun() ->
			db:execute(<<"INSERT INTO user(account, passwd) VALUES('test13', '123456')">>),
			db:execute(<<"INSERT INTO user(account, passwd) VALUES('test14', '123456')">>),
			throw(failed)
	end,
	db:transaction(F2).
test_dev() ->
	?DEV_MSG("test dev", []).

test_debug() ->
	?DEBUG_MSG("test debug", []).

test_info() ->
	?INFO_MSG("test info", []).

test_warning() ->
	?WARNING_MSG("test warning", []).

test_error() ->
	?ERROR_MSG("test error", []).

test_critical() ->
	?CRITICAL_MSG("test critical", []).
start() ->
	{ok,File}=file:open("test.txt",[write]),
	lists:foreach(fun(Num) ->
		spawn(fun() -> test(File, Num, 2345) end),
		receive
			_ ->
				skip
		after 10 ->
			skip
		end
								end, lists:seq(1, 10)).

test(File, Num, Port) ->
	case gen_tcp:connect("127.0.0.1", Port, [binary, {packet, 4}, {reuseaddr, true}]) of
		{ok, _} ->
			io:format(File, "~p~n", [Num]),
			receive
				_Any ->
					io:format("Any: ~p~n", [_Any])
			end;
		_ ->
			io:format("error~n")
	end.